

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="CodeHope">
  <meta name="author" content="CodeHope">
  <meta name="keywords" content="希望">
  <title>FLASK框架 - CodeHope</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      
        
          
          
          
        
        <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.22.0/themes/prism-tomorrow.min.css" />
      
      
        <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.22.0/plugins/line-numbers/prism-line-numbers.min.css" />
      
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"quancundexiwang.wang","root":"/","version":"1.8.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":true,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"onlypost":true},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"Mi65hxq7VAFUDwOLeIGAOgiV-gzGzoHsz","app_key":"hMuhiD4FRqhns4giqLiEH9HG","server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
  <header style="height: 80vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>CodeHope</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                村头
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                我
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607883712275&di=6a6e772459d71fb960b2f2a56321d531&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180514%2F91b8034f3e8c499aa660aa7c8dacf7cf.png') repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="FLASK框架">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-12-11 17:16" pubdate>
        2020年12月11日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      7.8k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      122
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
            <div class="scroll-down-bar">
              <i class="iconfont icon-arrowdown"></i>
            </div>
          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">FLASK框架</h1>
            
            <div class="markdown-body">
              <p>FLASK WEB框架</p>
<h2 id="1-环境安装"><a href="#1-环境安装" class="headerlink" title="1.环境安装"></a>1.环境安装</h2><h3 id="1-1修改pip的镜像地址（mac）"><a href="#1-1修改pip的镜像地址（mac）" class="headerlink" title="1.1修改pip的镜像地址（mac）"></a>1.1修改pip的镜像地址（mac）</h3><div class="code-wrapper"><pre class="line-numbers language-none"><code class="language-none">▶ cd ~
▶ mkdir .pip
▶ touch pip.conf
▶ vim pip.conf<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>复制进去</p>
<div class="code-wrapper"><pre class="line-numbers language-none"><code class="language-none">[global]
index-url&#x3D;http:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;pypi&#x2F;simple&#x2F;
[install]
trusted-host&#x3D;mirrors.aliyun.com<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>查看是否配置完成</p>
<div class="code-wrapper"><pre class="line-numbers language-none"><code class="language-none">▶ pip3 config list
global.index-url&#x3D;&#39;http:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;pypi&#x2F;simple&#x2F;&#39;
install.trusted-host&#x3D;&#39;mirrors.aliyun.com&#39;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<h3 id="1-2-安装flask"><a href="#1-2-安装flask" class="headerlink" title="1.2 安装flask"></a>1.2 安装flask</h3><p>创建项目目录</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">▶ cd PycharmProjects
▶ mkdir code.flask.v1
▶ cd code.flask.v1<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>创建项目虚拟环境：Python3，虚拟环境已经成为内置模块，可以直接通过如下命令来创建它：</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">▶ python3 -m venv venv<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<blockquote>
<p>如果你使用的Python版本低于3.4（包括2.7版本），则不会默认支持虚拟环境。 对于这些版本的Python，在创建虚拟环境之前，需要下载并安装称为virtualenv的第三方工具。</p>
</blockquote>
<p>创建完毕之后还需要激活才能够进入这个虚拟环境。 要激活你的全新虚拟环境，需使用以下命令：</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">▶ source venv&#x2F;bin&#x2F;activate

# 进入虚拟机环境⬇️

(venv) 
▶ ~&#x2F;PycharmProjects&#x2F;code.flask.v1    <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>成功创建和激活了虚拟环境之后，你可以安装Flask了，命令如下：</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">(venv) 
▶ ~&#x2F;PycharmProjects&#x2F;code.flask.v1 
▶ pip install flask<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<h2 id="2-第一个Flask程序"><a href="#2-第一个Flask程序" class="headerlink" title="2.第一个Flask程序"></a>2.第一个<code>Flask</code>程序</h2><blockquote>
<p>应用程序是存在于包中的。 在Python中，包含<strong>init</strong>.py文件的子目录被视为一个可导入的包。 当你导入一个包时，<strong>init</strong>.py会执行并定义这个包暴露给外界的属性。</p>
</blockquote>
<p>那就创建一个名为app的包来存放整个应用吧。记得切换到<code>code.flask.v1</code>目录下，并执行如下命令：</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">▶ mkdir app
▶ touch __init__.py #如果你用pycharm直接创建包就行<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></div>
<p>输入如下的代码：</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask

app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span>

<span class="token comment"># name参数，表示flask应用的主模块或者包的位置，使用该参数确定应用位置。</span>

<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span> <span class="token comment"># 匹配url</span>
<span class="token keyword">def</span> <span class="token function">hello_world</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> <span class="token string">'Hello world,Hello flask'</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">~&#x2F;PycharmProjects&#x2F;code.flask.v1                                                                                                                                                                          
▶ export FLASK_APP&#x3D;app.py  # 运行之前，需要通过设置FLASK_APP环境变量告诉Flask如何导入它
▶ flask run
# 执行成功
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http:&#x2F;&#x2F;127.0.0.1:5000&#x2F; (Press CTRL+C to quit)
127.0.0.1 - - [11&#x2F;Dec&#x2F;2020 08:14:54] &quot;GET &#x2F; HTTP&#x2F;1.1&quot; 200 -
127.0.0.1 - - [11&#x2F;Dec&#x2F;2020 08:14:54] &quot;GET &#x2F;favicon.ico HTTP&#x2F;1.1&quot; 404 -<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201211083043.png" srcset="/img/loading.gif" alt="image-20201211083037645"></p>
<p>启动成功。</p>
<p>或者你在代码中写上运行函数，然后执行代码文件也可以,效果是一样的。<strong>（v1.0不推荐使用）</strong></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask

app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">hello_world</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> <span class="token string">'Hello world,Hello flask'</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    app<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<h2 id="3-flask启动项以及调试"><a href="#3-flask启动项以及调试" class="headerlink" title="3. flask启动项以及调试"></a>3. <code>flask</code>启动项以及调试</h2><h3 id="3-1-指定ip及其端口号"><a href="#3-1-指定ip及其端口号" class="headerlink" title="3.1 指定ip及其端口号"></a>3.1 指定ip及其端口号</h3><div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">(venv) 
~&#x2F;PycharmProjects&#x2F;code.flask.v1                                                                                                                                                                       
▶ flask run --host&#x3D;0.0.0.0 --port&#x3D;0520

 * Serving Flask app &quot;app.py&quot;
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http:&#x2F;&#x2F;0.0.0.0:520&#x2F; (Press CTRL+C to quit)
127.0.0.1 - - [11&#x2F;Dec&#x2F;2020 08:54:45] &quot;GET &#x2F; HTTP&#x2F;1.1&quot; 200 -
127.0.0.1 - - [11&#x2F;Dec&#x2F;2020 08:54:45] &quot;GET &#x2F;favicon.ico HTTP&#x2F;1.1&quot; 404 -
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>简写</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">(venv) 
~&#x2F;PycharmProjects&#x2F;code.flask.v1                                                                                                                                                                          
▶ flask run -h 0.0.0.0 -p 0520  

 * Serving Flask app &quot;app.py&quot;
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http:&#x2F;&#x2F;0.0.0.0:520&#x2F; (Press CTRL+C to quit)
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<h3 id="3-2-开启调试模式"><a href="#3-2-开启调试模式" class="headerlink" title="3.2 开启调试模式"></a>3.2 开启调试模式</h3><ul>
<li>代码修改后自动重启,通过设置<code>FLASK_ENV=development</code></li>
</ul>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">(venv) 
~&#x2F;PycharmProjects&#x2F;code.flask.v1                                                                                                                                                                                                        
▶ export FLASK_ENV&#x3D;development               
(venv) 
~&#x2F;PycharmProjects&#x2F;code.flask.v1                                                                                                                                                                                                        
▶ flask run -h 0.0.0.0 -p 0520
 * Serving Flask app &quot;app.py&quot; (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http:&#x2F;&#x2F;0.0.0.0:520&#x2F; (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 659-583-679
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>我们可以看到 <code> Debug mode</code> 在之前的演示中都是<code>off</code>现在是<code>on</code>代表已经启动起来了。<code>Environment</code>也从<code>production</code>变成了<code>development</code></p>
<p>当我们修改了代码,就不用去重启项目，就能看到最新的代码效果了。</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">Debugger is active!
* Debugger PIN: 659-583-679
* Detected change in &#39;&#x2F;Users&#x2F;codehope&#x2F;PycharmProjects&#x2F;code.flask.v1&#x2F;app&#x2F;__init__.py&#39;, reloading
* Restarting with stat<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<h2 id="4-应用路由配置"><a href="#4-应用路由配置" class="headerlink" title="4.应用路由配置"></a>4.应用路由配置</h2><h3 id="4-1-装饰器"><a href="#4-1-装饰器" class="headerlink" title="4.1 装饰器"></a>4.1 装饰器</h3><ul>
<li><p>语法规则</p>
<p><code>@app.route(url,methods)</code></p>
</li>
<li><p>参数解释</p>
<p>url:匹配的<code>url</code>地址</p>
<p>methods：支持的请求方式<code>[GET,POST...]</code></p>
</li>
<li><p>示例</p>
<p><code>@app.route(&#39;/login&#39;,methods=[&#39;GET&#39;,&#39;POST])</code></p>
</li>
</ul>
<h3 id="4-2-使用api配置-不常用"><a href="#4-2-使用api配置-不常用" class="headerlink" title="4.2 使用api配置(不常用)"></a>4.2 使用api配置(不常用)</h3><ul>
<li><p>语法规则</p>
<p><code>app.add_url_rule(url,url_name,view_name)</code></p>
</li>
<li><p>参数解释</p>
<p>url:匹配的<code>url</code>地址</p>
<p>url_name：给<code>url</code>路径的命名</p>
<p>view_name:视图函数方法名称</p>
</li>
<li><p>示例</p>
<p><code>app.add_url_rule(&quot;/api_router&quot;, &#39;api_method&#39;, hello_world)</code></p>
</li>
</ul>
<h3 id="4-3-路由匹配规则"><a href="#4-3-路由匹配规则" class="headerlink" title="4.3 路由匹配规则"></a>4.3 路由匹配规则</h3><ul>
<li><p>匹配整个文字</p>
<p><code>@app.route(&#39;/hell0&#39;)</code></p>
</li>
<li><p>传递参数</p>
<p><code>@app.route(&#39;/user/&lt;username&gt;&#39;)</code></p>
</li>
<li><p>指定参数类型</p>
<p><code> @app.route(&#39;/userInfo/&lt;int:user_Id&gt;&#39;)</code></p>
<p>URL参数类型</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201211125031.png" srcset="/img/loading.gif" alt="image-20201211125026218"></p>
</li>
</ul>
<ul>
<li>查看URL规则列表 <code>app.url_map</code></li>
</ul>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">print</span><span class="token punctuation">(</span>app<span class="token punctuation">.</span>url_map<span class="token punctuation">)</span>

<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">></span>

Map<span class="token punctuation">(</span>
  <span class="token punctuation">[</span> 
    <span class="token operator">&lt;</span>Rule <span class="token string">'/'</span> <span class="token punctuation">(</span>HEAD<span class="token punctuation">,</span> GET<span class="token punctuation">,</span> OPTIONS<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> hello_world<span class="token operator">></span><span class="token punctuation">,</span> <span class="token comment"># 我们定义的路由</span>
 		<span class="token operator">&lt;</span>Rule <span class="token string">'/static/&lt;filename>'</span> <span class="token punctuation">(</span>HEAD<span class="token punctuation">,</span> GET<span class="token punctuation">,</span> OPTIONS<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> static<span class="token operator">></span> <span class="token comment"># 服务器静态资源</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<ul>
<li>URL地址逆向解析（根据名称解析成<code>URL字符串</code>）<ul>
<li><code>url_for(url_name,**kwargs)</code></li>
<li>静态文件：<code>url_for(&#39;static&#39;,filename= &#39;xxxx&#39;)</code></li>
</ul>
</li>
</ul>
<h3 id="4-4视图函数中获取页面传值"><a href="#4-4视图函数中获取页面传值" class="headerlink" title="4.4视图函数中获取页面传值"></a>4.4视图函数中获取页面传值</h3><ul>
<li><p>URL中 的值</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/list'</span><span class="token punctuation">)</span> <span class="token comment">#用于默认值不传的情况，去匹配路由</span>
<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/list/&lt;int:page>'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">getList</span><span class="token punctuation">(</span>page<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token comment"># 使用默认值</span>
    <span class="token keyword">return</span> <span class="token string">'这是第&#123;&#125;页数据'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>page<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div></li>
<li><p>GET/POST请求获取<code>request</code></p>
<p>引入<code>request模块</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> request<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/test_req'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">getUserInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    arg <span class="token operator">=</span> request<span class="token punctuation">.</span>args<span class="token punctuation">;</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>arg<span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token string">'test.,request'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>访问URL <code>http://0.0.0.0:520/test_req?id=1&amp;name=常昊</code></p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">127.0.0.1 - - [11&#x2F;Dec&#x2F;2020 14:49:27] &quot;GET &#x2F;test_req?id&#x3D;1&amp;name&#x3D;常昊 HTTP&#x2F;1.1&quot; 200 -
# 拿到的get传的参数
ImmutableMultiDict([(&#39;id&#39;, &#39;1&#39;), (&#39;name&#39;, &#39;常昊&#39;)])<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>将这些值取出来</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/test_req'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">getUserInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    arg <span class="token operator">=</span> request<span class="token punctuation">.</span>args<span class="token punctuation">;</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>arg<span class="token punctuation">)</span>
    userId <span class="token operator">=</span> arg<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'id'</span><span class="token punctuation">)</span>
    userName <span class="token operator">=</span> arg<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'name'</span><span class="token punctuation">)</span>
    address <span class="token operator">=</span> arg<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'address'</span><span class="token punctuation">,</span> <span class="token string">'默认地址'</span><span class="token punctuation">)</span><span class="token comment"># 如果没有匹配到对应字段可以给默认值</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f'名字为</span><span class="token interpolation"><span class="token punctuation">&#123;</span>userName<span class="token punctuation">&#125;</span></span><span class="token string">,地址为</span><span class="token interpolation"><span class="token punctuation">&#123;</span>address<span class="token punctuation">&#125;</span></span><span class="token string">,身份号码为</span><span class="token interpolation"><span class="token punctuation">&#123;</span>userId<span class="token punctuation">&#125;</span></span><span class="token string">'</span></span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token string">'test.,request'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>访问结果</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">ImmutableMultiDict([(&#39;id&#39;, &#39;1&#39;), (&#39;name&#39;, &#39;常昊&#39;)])
名字为常昊,地址为默认地址,身份号码为1
127.0.0.1 - - [11&#x2F;Dec&#x2F;2020 14:56:11] &quot;GET &#x2F;test_req?id&#x3D;1&amp;name&#x3D;常昊 HTTP&#x2F;1.1&quot; 200 -<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p><code>request</code>能获取客户端请求的很多信息</p>
</li>
</ul>
<h3 id="4-5-请求钩子"><a href="#4-5-请求钩子" class="headerlink" title="4.5 请求钩子"></a>4.5 请求钩子</h3><blockquote>
<p>我们可以规定当客户请求服务器的时候，就默认调用一些公用的钩子函数,来完成某些功能。比如验证用户登陆，每一个接口都要求登陆后才能访问。</p>
</blockquote>
<ul>
<li><code>before_first_request</code>:在处理第一个请求前执行。比如链接数据库操作</li>
<li><code>before_request</code>:在每次请求前执行。比如权限校验，ip连接</li>
<li><code>after_request</code>:每次请求之后调用，前提是没有未处理的异常抛出(有异常，就不调用)</li>
<li><code>teardown_request</code>:每次请求之后调用，即使有未处理的异常抛出（有没有异常都调用）</li>
</ul>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> request

app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>before_first_request</span>
<span class="token keyword">def</span> <span class="token function">server_first_request</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'服务器启动后，第一个请求执行了我！!'</span><span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>before_request</span>
<span class="token keyword">def</span> <span class="token function">before_request</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'每次请求都要执行我！!'</span><span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>teardown_request</span>
<span class="token keyword">def</span> <span class="token function">teardown</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> err<span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
        <span class="token keyword">return</span> err
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'视图函数执行完'</span><span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>after_request</span>
<span class="token keyword">def</span> <span class="token function">after_request</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'请求执行完成之后'</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> response


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/index'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># raise Exception("模拟错误")</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'执行视图函数'</span><span class="token punctuation">)</span> 
    <span class="token keyword">return</span> <span class="token string">'result'</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>访问两次URL <code>http://0.0.0.0:520/user/1</code></p>
<div class="code-wrapper"><pre class="line-numbers language-SHELL" data-language="SHELL"><code class="language-SHELL">127.0.0.1 - - [11&#x2F;Dec&#x2F;2020 15:53:23] &quot;GET &#x2F;index HTTP&#x2F;1.1&quot; 200 -
服务器启动后，第一个请求执行了我！!
每次请求都要执行我！!
执行视图函数
请求执行完成之后
视图函数执行完

每次请求都要执行我！!
执行视图函数
请求执行完成之后
视图函数执行完
127.0.0.1 - - [11&#x2F;Dec&#x2F;2020 15:53:29] &quot;GET &#x2F;index HTTP&#x2F;1.1&quot; 200 -<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<h3 id="4-6-请求响应体"><a href="#4-6-请求响应体" class="headerlink" title="4.6 请求响应体"></a>4.6 请求响应体</h3><ul>
<li><p>可以是字符串</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/index'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> <span class="token string">'result'</span> <span class="token comment"># 纯字符串的响应</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>


</li>
</ul>
<ul>
<li><p>可以是元祖 <code>make_response</code></p>
<ul>
<li>1.response-响应内容</li>
<li>2.status-状态码</li>
<li>3.headers-响应头</li>
</ul>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> make_response


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/index'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># raise Exception("模拟错误")</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'执行视图函数'</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> make_response<span class="token punctuation">(</span>
        <span class="token punctuation">&#123;</span> <span class="token comment"># 响应内容</span>
            <span class="token string">'data'</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
        <span class="token punctuation">&#125;</span>
        <span class="token punctuation">,</span> <span class="token number">203</span> <span class="token comment"># 状态码</span>
    <span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>访问 <code>http://0.0.0.0:520/index</code></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201211161040.png" srcset="/img/loading.gif" alt="image-20201211161040830"></p>
<p>你也可以这样写</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/index'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># raise Exception("模拟错误")</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'执行视图函数'</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token punctuation">&#123;</span><span class="token string">'data'</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span> <span class="token comment"># 响应数据</span>
  					<span class="token number">201</span><span class="token punctuation">,</span> <span class="token comment"># 响应状态码</span>
    				<span class="token punctuation">&#123;</span><span class="token string">"user_id"</span><span class="token punctuation">:</span> <span class="token number">1000</span><span class="token punctuation">&#125;</span> <span class="token comment"># 响应headers</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<img src="http://qiniuyun.quancundexiwang.wang/20201211161502.png" srcset="/img/loading.gif" alt="image-20201211161502169" style="zoom:100%;" />



</li>
</ul>
<h3 id="4-7-redirect请求重定向"><a href="#4-7-redirect请求重定向" class="headerlink" title="4.7 redirect请求重定向"></a>4.7 <code>redirect</code>请求重定向</h3><div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span>make_response<span class="token punctuation">,</span> redirect <span class="token comment">#引入包</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span> <span class="token comment"># 访问</span>
<span class="token keyword">def</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> redirect<span class="token punctuation">(</span><span class="token string">"/index"</span><span class="token punctuation">)</span> <span class="token comment"># 重定向</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/index'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># raise Exception("模拟错误")</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'执行视图函数'</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> make_response<span class="token punctuation">(</span>
        <span class="token punctuation">&#123;</span>
            <span class="token string">'data'</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
        <span class="token punctuation">&#125;</span>
        <span class="token punctuation">,</span> <span class="token number">203</span>
    <span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>请求URL:<code>http://0.0.0.0:520/</code> 被重定向到<code>http://0.0.0.0:520/index</code></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201211162845.png" srcset="/img/loading.gif" alt="image-20201211162845047"></p>
<h3 id="4-8-abort处理错误"><a href="#4-8-abort处理错误" class="headerlink" title="4.8 abort处理错误"></a>4.8 <code>abort</code>处理错误</h3><blockquote>
<p>使用<code>abort</code>指向错误页面（不需要return</p>
</blockquote>
<p>404</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/not_found'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">go_not_found</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    abort<span class="token punctuation">(</span><span class="token number">404</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>访问 <code>http://0.0.0.0:520/not_found</code></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201211163431.png" srcset="/img/loading.gif" alt="image-20201211163431932"></p>
<p>403</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/forbidden'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">go_forbidden</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    abort<span class="token punctuation">(</span><span class="token number">403</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>访问<code>http://0.0.0.0:520/forbidden</code></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201211163356.png" srcset="/img/loading.gif" alt="image-20201211163356488"></p>
<p>500</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/servererror'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">go_servererror</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    abort<span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>访问： <code>http://0.0.0.0:520/servererror</code></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201211165926.png" srcset="/img/loading.gif" alt="image-20201211165926737"></p>
<p>我们可以使用<code>errorhandler(status)</code>自定义页面重定向</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python">
<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>errorhandler</span><span class="token punctuation">(</span><span class="token number">404</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">NOT_FOUND</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token string">'您访问的页面不存在'</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>errorhandler</span><span class="token punctuation">(</span><span class="token number">403</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">NOT_FOUND</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token string">'您没有权限访问该页面，请联系客服'</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>errorhandler</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">NOT_FOUND</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token string">'服务器错误，请联系管理员'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201211170020.png" srcset="/img/loading.gif" alt="image-20201211170020721"></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201211170628.png" srcset="/img/loading.gif"></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201211173150.png" srcset="/img/loading.gif"></p>
<h2 id="5-FLASK中的模版template"><a href="#5-FLASK中的模版template" class="headerlink" title="5.FLASK中的模版template"></a>5.FLASK中的模版<code>template</code></h2><h3 id="5-1-概念"><a href="#5-1-概念" class="headerlink" title="5.1 概念"></a>5.1 概念</h3><blockquote>
<p>视图函数的主要作用是生成请求的响应，这是最简单的请求。实际上，视图函数有两个作用：处理业务逻辑和返回响应内容。在大型应用中，把业务逻辑和表现内容放在一起，会增加代码的复杂度和维护成本。本节学到的模板，它的作用即是承担视图函数的另一个作用，即返回响应内容。</p>
</blockquote>
<blockquote>
<p>模板其实是一个包含响应文本的文件，其中用占位符(变量)表示动态部分，告诉模板引擎其具体的值需要从使用的数据中获取<br>使用真实值替换变量，再返回最终得到的字符串，这个过程称为“渲染”<br>Flask是使用 Jinja2 这个模板引擎来渲染模板</p>
</blockquote>
<p>使用模板的好处：</p>
<blockquote>
<p>视图函数只负责业务逻辑和数据处理(业务逻辑方面)<br>而模板则取到视图函数的数据结果进行展示(视图展示方面)<br>代码结构清晰，耦合度低</p>
</blockquote>
<p>文件目录结构</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">|____app
| |______init__.py
| |______templates
| | |______index.html
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>app&gt;__init__.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> render_template

app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span>

<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">renderHome</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'./index.html'</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>templates&gt;index.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-none"><code class="language-none">&lt;!DOCTYPE html&gt;
&lt;html lang&#x3D;&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;
    &lt;title&gt;Title&lt;&#x2F;title&gt;
&lt;&#x2F;head&gt;
&lt;body&gt;
&lt;h1&gt;Home Page&lt;&#x2F;h1&gt;
&lt;&#x2F;body&gt;
&lt;&#x2F;html&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>访问URL:<code>http://0.0.0.0:520/</code></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212002527.png" srcset="/img/loading.gif" alt="image-20201212002527235"></p>
<h3 id="5-2-默认配置"><a href="#5-2-默认配置" class="headerlink" title="5.2 默认配置"></a>5.2 默认配置</h3><ul>
<li><code>template_folder = &#39;templates&#39;</code> - 默认的模版目录</li>
<li><code>render_template()-(x)html</code>自动转义</li>
</ul>
<h3 id="5-3-模版中使用变量"><a href="#5-3-模版中使用变量" class="headerlink" title="5.3 模版中使用变量"></a>5.3 模版中使用变量</h3><h4 id="5-3-1-简单数据类型的变量使用"><a href="#5-3-1-简单数据类型的变量使用" class="headerlink" title="5.3.1 简单数据类型的变量使用"></a>5.3.1 简单数据类型的变量使用</h4><div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> render_template

app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">renderHome</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    name <span class="token operator">=</span> <span class="token string">'常昊'</span>
    age <span class="token operator">=</span> <span class="token number">23</span>
    hometown <span class="token operator">=</span> <span class="token string">'中国'</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'./index.html'</span><span class="token punctuation">,</span> name<span class="token operator">=</span>name<span class="token punctuation">,</span> age<span class="token operator">=</span>age<span class="token punctuation">,</span> hometown<span class="token operator">=</span>hometown<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>Title<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">></span></span><span class="token style"><span class="token language-css">
        <span class="token selector">h1,p</span><span class="token punctuation">&#123;</span>
            <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">></span></span>Home Page<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>个人介绍<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token comment">&lt;!--很像vue中的插值表达式--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>我的名字是&#123;&#123;name&#125;&#125;我的年龄&#123;&#123;age&#125;&#125;,我的故乡在&#123;&#123;hometown&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212005623.png" srcset="/img/loading.gif" alt="image-20201212005623403"></p>
<h4 id="5-3-2-复杂数据类型的变量使用"><a href="#5-3-2-复杂数据类型的变量使用" class="headerlink" title="5.3.2 复杂数据类型的变量使用"></a>5.3.2 复杂数据类型的变量使用</h4><ul>
<li><p><code>DICT</code>字典数据类型的使用</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> render_template

app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span>

<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">renderHome</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    name <span class="token operator">=</span> <span class="token string">'常昊'</span>
    age <span class="token operator">=</span> <span class="token number">23</span>
    hometown <span class="token operator">=</span> <span class="token string">'中国'</span>
    userDetails <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
        <span class="token string">'gender'</span><span class="token punctuation">:</span> <span class="token string">'male'</span><span class="token punctuation">,</span>
        <span class="token string">'work'</span><span class="token punctuation">:</span> <span class="token string">"coder"</span><span class="token punctuation">,</span>
        <span class="token string">'born_area'</span><span class="token punctuation">:</span> <span class="token punctuation">&#123;</span>
            <span class="token string">'province'</span><span class="token punctuation">:</span> <span class="token string">"He Nan"</span><span class="token punctuation">,</span>
            <span class="token string">'city'</span><span class="token punctuation">:</span> <span class="token string">'Zheng Zhou'</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'./index.html'</span><span class="token punctuation">,</span> name<span class="token operator">=</span>name<span class="token punctuation">,</span> age<span class="token operator">=</span>age<span class="token punctuation">,</span> hometown<span class="token operator">=</span>hometown<span class="token punctuation">,</span> userDetails<span class="token operator">=</span>userDetails<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>Title<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">></span></span><span class="token style"><span class="token language-css">
        <span class="token selector">h1, p</span> <span class="token punctuation">&#123;</span>
            <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">></span></span>Home Page<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>个人介绍<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token comment">&lt;!--很像vue中的插值表达式--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>我的名字是&#123;&#123;name&#125;&#125;我的年龄&#123;&#123;age&#125;&#125;,我的故乡在&#123;&#123;hometown&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>信息详情<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>&#123;&#123;userDetails&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>P</span><span class="token punctuation">></span></span>性别：&#123;&#123;userDetails['gender']&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>P</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>P</span><span class="token punctuation">></span></span>工作：&#123;&#123;userDetails.work&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>P</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>P</span><span class="token punctuation">></span></span>出生的省份&#123;&#123;userDetails['born_area'].province&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>P</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>P</span><span class="token punctuation">></span></span>出生的城市：&#123;&#123;userDetails['born_area'].city&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>P</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212005643.png" srcset="/img/loading.gif" alt="image-20201212005643732"></p>
</li>
<li><p>元祖和列表</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> render_template

app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">renderHome</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    name <span class="token operator">=</span> <span class="token string">'常昊'</span>
    age <span class="token operator">=</span> <span class="token number">23</span>
    hometown <span class="token operator">=</span> <span class="token string">'中国'</span>
    userDetails <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
        <span class="token string">'gender'</span><span class="token punctuation">:</span> <span class="token string">'male'</span><span class="token punctuation">,</span>
        <span class="token string">'work'</span><span class="token punctuation">:</span> <span class="token string">"coder"</span><span class="token punctuation">,</span>
        <span class="token string">'born_area'</span><span class="token punctuation">:</span> <span class="token punctuation">&#123;</span>
            <span class="token string">'province'</span><span class="token punctuation">:</span> <span class="token string">"He Nan"</span><span class="token punctuation">,</span>
            <span class="token string">'city'</span><span class="token punctuation">:</span> <span class="token string">'Zheng Zhou'</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
    hobby <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'羽毛球'</span><span class="token punctuation">,</span> <span class="token string">'唱歌'</span><span class="token punctuation">]</span>
    skills <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">'python'</span><span class="token punctuation">,</span> <span class="token string">'javascript'</span><span class="token punctuation">,</span> <span class="token string">'mysql'</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'./index.html'</span><span class="token punctuation">,</span>
                           name<span class="token operator">=</span>name<span class="token punctuation">,</span>
                           age<span class="token operator">=</span>age<span class="token punctuation">,</span>
                           hometown<span class="token operator">=</span>hometown<span class="token punctuation">,</span>
                           userDetails<span class="token operator">=</span>userDetails<span class="token punctuation">,</span>
                           hobby<span class="token operator">=</span>hobby<span class="token punctuation">,</span>
                           skills<span class="token operator">=</span>skills
                           <span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>Title<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">></span></span><span class="token style"><span class="token language-css">
        <span class="token selector">h1, p</span> <span class="token punctuation">&#123;</span>
            <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">></span></span>Home Page<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>个人介绍<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token comment">&lt;!--很像vue中的插值表达式--></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>我的名字是&#123;&#123;name&#125;&#125;我的年龄&#123;&#123;age&#125;&#125;,我的故乡在&#123;&#123;hometown&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>信息详情<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>&#123;&#123;userDetails&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>P</span><span class="token punctuation">></span></span>性别：&#123;&#123;userDetails['gender']&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>P</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>P</span><span class="token punctuation">></span></span>工作：&#123;&#123;userDetails.work&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>P</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>P</span><span class="token punctuation">></span></span>出生的省份&#123;&#123;userDetails['born_area'].province&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>P</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>P</span><span class="token punctuation">></span></span>出生的城市：&#123;&#123;userDetails['born_area'].city&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>P</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>个人爱好<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>&#123;&#123;hobby[0]&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>&#123;&#123;hobby[1]&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>&#123;&#123;hobby[2]&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>个人技能<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>&#123;&#123;skills[0]&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>&#123;&#123;skills[1]&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>&#123;&#123;skills[2]&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212010234.png" srcset="/img/loading.gif" alt="image-20201212010234374"></p>
<p><span style="color:red">上面这种情况如果我们的数据有几百几千个就不方便使用啦，所以我们学习一下<code>模版标签</code></span></p>
</li>
</ul>
<h3 id="5-4-模版语法"><a href="#5-4-模版语法" class="headerlink" title="5.4 模版语法"></a>5.4 模版语法</h3><p>分为三部分学习</p>
<ul>
<li><p>模版标签</p>
</li>
<li><p>过滤器</p>
</li>
<li><p>模版全局函数</p>
<h4 id="5-4-1-模版标签"><a href="#5-4-1-模版标签" class="headerlink" title="5.4.1 模版标签"></a>5.4.1 模版标签</h4></li>
</ul>
<blockquote>
<p>模版中写我们的python逻辑的代码</p>
</blockquote>
<ul>
<li>条件判断</li>
<li>循环</li>
<li>赋值</li>
<li>……</li>
</ul>
<p><span style="color:red;font-size: 22px">语法</span>：</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">1.####
&#123;% tag %&#125;

2.#### 
&#123;% tag %&#125;
	内容
&#123;% endtag %&#125;
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<h5 id="a-条件表达式"><a href="#a-条件表达式" class="headerlink" title="a.条件表达式"></a><span style="color:red;font-size: 22px">a.条件表达式</span></h5><div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% if condition a %&#125;
	满足条件a展示
&#123;% elif condition b %&#125;
	满足条件a展示
&#123;% else %&#125;
	都不满足展示
&#123;% endif %&#125; 
&lt;!--endif 结束if语句--&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>根据上面的案例修改代码</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;p&gt;我的名字是&#123;&#123;name&#125;&#125;&lt;&#x2F;p&gt;
&lt;p&gt;我的年龄&#123;&#123;age&#125;&#125;&lt;&#x2F;p&gt;
		&#123;% if age&gt;18 %&#125;
&lt;p&gt;满18岁是个成年人了&lt;&#x2F;p&gt;
		&#123;% else %&#125;
&lt;p&gt;未满18岁，还是个小p孩&#x2F;p&gt;
    &#123;% endif %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212012508.png" srcset="/img/loading.gif" alt="image-20201212012508527"></p>
<h5 id="b-循环语句"><a href="#b-循环语句" class="headerlink" title="b.循环语句"></a><span style="color:red;font-size: 22px">b.循环语句</span></h5><p><span style="color:red;">语法</span></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% for key,value in data.items()%&#125;
&#123;&#123;key&#125;&#125;:&#123;&#123;value&#125;&#125;

&#123;% else %&#125;
数据为空

&#123;% endfor %&#125;
&lt;!--endfor 结束for语句--&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>修改上面案例</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;p&gt;个人爱好&lt;&#x2F;p&gt;
&#123;% for item in hobby %&#125;
&lt;li&gt;&#123;&#123;item&#125;&#125;&lt;&#x2F;li&gt;
&#123;% else %&#125;
&lt;p&gt;没有兴趣爱好&lt;&#x2F;p&gt;
&#123;% endfor %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212013333.png" srcset="/img/loading.gif" alt="image-20201212013333486"></p>
<p><span style="color:red;">如果list是个空数组</span></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment"># hobby = ['羽毛球', '唱歌']</span>
hobby <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></div>


<p><img src="http://qiniuyun.quancundexiwang.wang/20201212013453.png" srcset="/img/loading.gif" alt="image-20201212013453628"></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/brother'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">showBrother</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    NAME <span class="token operator">=</span> <span class="token string">'name'</span>
    AGE <span class="token operator">=</span> <span class="token string">'age'</span>
    ADDRESS <span class="token operator">=</span> <span class="token string">'address'</span>
    brothers <span class="token operator">=</span> <span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"张三"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">22</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"王武"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">29</span><span class="token punctuation">,</span> ADDRESS<span class="token punctuation">:</span> <span class="token string">"通县"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"赵四"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">38</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"刘能"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">36</span><span class="token punctuation">,</span> ADDRESS<span class="token punctuation">:</span> <span class="token string">"铁岭"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"大脑袋"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">44</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"王麻子"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">36</span><span class="token punctuation">,</span> ADDRESS<span class="token punctuation">:</span> <span class="token string">"牛栏山"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'brother.html'</span><span class="token punctuation">,</span> brothers<span class="token operator">=</span>brothers<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>brother.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;!DOCTYPE html&gt;
&lt;html lang&#x3D;&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;
    &lt;title&gt;Title&lt;&#x2F;title&gt;
    &lt;style&gt;
        .line1 &#123;
            background: rebeccapurple;
            color: #fff;
        &#125;

        .line2 &#123;
            background: yellow;
            color: #000;
        &#125;
    &lt;&#x2F;style&gt;
&lt;&#x2F;head&gt;
&lt;body&gt;
&#123;% for bro in brothers %&#125;
&lt;li class&#x3D;&quot;&#123;&#123;loop.cycle(&#39;line1&#39;,&#39;line2&#39;)&#125;&#125;&quot;&gt;
    &#123;% for key,value in bro.items() %&#125;
    属性序号:&#123;&#123;loop.index&#125;&#125;
    &#123;&#123;key&#125;&#125;:&#123;&#123;value&#125;&#125; &amp;nbsp
    &#123;% endfor %&#125;
&lt;&#x2F;li&gt;
&lt;p&gt;第&#123;&#123;loop.index&#125;&#125;个,编号是&#123;&#123;loop.index0&#125;&#125;，共&#123;&#123;loop.length&#125;&#125;名兄弟&lt;&#x2F;p&gt;
&#123;% endfor %&#125;
&lt;&#x2F;body&gt;
&lt;&#x2F;html&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212020229.png" srcset="/img/loading.gif" alt="image-20201212020229217"></p>
<h5 id="c-设置flask扩展"><a href="#c-设置flask扩展" class="headerlink" title="c.设置flask扩展"></a>c.设置<code>flask</code>扩展</h5><p><span style="color:red;">我们看到有的人有<code>address属性有的没有，我们想让展示统一，全部只展示前两个属性，那么就是属性遍历的时候index&gt;2的跳过循环使用</code>continue/break`关键字</span></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token operator">&lt;</span>li <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"&#123;&#123;loop.cycle('line1','line2')&#125;&#125;"</span><span class="token operator">></span>
    <span class="token punctuation">&#123;</span><span class="token operator">%</span> <span class="token keyword">for</span> key<span class="token punctuation">,</span>value <span class="token keyword">in</span> bro<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">%</span><span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#123;</span><span class="token operator">%</span> <span class="token keyword">if</span> loop<span class="token punctuation">.</span>index <span class="token operator">></span> <span class="token number">2</span> <span class="token operator">%</span><span class="token punctuation">&#125;</span> 如果循环属性时，循环index<span class="token operator">></span><span class="token number">2</span>就跳过了
    <span class="token punctuation">&#123;</span><span class="token operator">%</span> <span class="token keyword">continue</span> <span class="token operator">%</span><span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#123;</span><span class="token operator">%</span> endif <span class="token operator">%</span><span class="token punctuation">&#125;</span>
    属性序号<span class="token punctuation">:</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#123;</span>loop<span class="token punctuation">.</span>index<span class="token punctuation">&#125;</span><span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#123;</span><span class="token punctuation">&#123;</span>key<span class="token punctuation">&#125;</span><span class="token punctuation">&#125;</span><span class="token punctuation">:</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#123;</span>value<span class="token punctuation">&#125;</span><span class="token punctuation">&#125;</span> <span class="token operator">&amp;</span>nbsp
    <span class="token punctuation">&#123;</span><span class="token operator">%</span> endfor <span class="token operator">%</span><span class="token punctuation">&#125;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>执行后报错</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python">jinja2<span class="token punctuation">.</span>exceptions<span class="token punctuation">.</span>TemplateSyntaxError
jinja2<span class="token punctuation">.</span>exceptions<span class="token punctuation">.</span>TemplateSyntaxError<span class="token punctuation">:</span> Encountered unknown tag <span class="token string">'continue'</span><span class="token punctuation">.</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></div>
<p>因为``jinja<code>模版语法的循环，目前不支持</code>break/continue`关键字，所以我们要对flask进行扩展添加</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python">app<span class="token punctuation">.</span>jinja_env<span class="token punctuation">.</span>add_extension<span class="token punctuation">(</span><span class="token string">'jinja2.ext.loopcontrols'</span><span class="token punctuation">)</span>
<span class="token comment"># flask应用，添加jinja2的流程控制的扩展</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></div>
<p>重新执行，完成了我们的想要的效果。</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212021221.png" srcset="/img/loading.gif" alt="image-20201212021221247"></p>
<h5 id="d-去除空格"><a href="#d-去除空格" class="headerlink" title="d.去除空格"></a>d.去除空格</h5><p>语法:模版代码块的开始或者结束加 <code>-</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% tag -%&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p>去除空格前的源码</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212022146.png" srcset="/img/loading.gif" alt="image-20201212022146192"></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;li class&#x3D;&quot;&#123;&#123;loop.cycle(&#39;line1&#39;,&#39;line2&#39;)&#125;&#125;&quot;&gt;
    &#123;% for key,value in bro.items() -%&#125;
    &#123;% if loop.index &gt; 2 %&#125;
    &#123;% continue %&#125;
    &#123;% endif %&#125;
    属性序号:&#123;&#123;loop.index&#125;&#125;
    &#123;&#123;key&#125;&#125;:&#123;&#123;value&#125;&#125; &amp;nbsp
    &#123;% endfor %&#125;
&lt;&#x2F;li&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212022846.png" srcset="/img/loading.gif" alt="image-20201212022846235"></p>
<h5 id="e-添加注释"><a href="#e-添加注释" class="headerlink" title="e .添加注释"></a>e .添加注释</h5><blockquote>
<p>通过模版添加的注释不会显示在html源码中</p>
</blockquote>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2"> &lt;!--原生html注释--&gt;
&#123;# jinja2模版注释 #&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212022644.png" srcset="/img/loading.gif" alt="image-20201212022644792"></p>
<h5 id="f-设置变量，赋值操作"><a href="#f-设置变量，赋值操作" class="headerlink" title="f 设置变量，赋值操作"></a>f 设置变量，赋值操作</h5><blockquote>
<p>先设置后使用,可以通过import导入</p>
</blockquote>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% set key,value &#x3D; (1,2) %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<blockquote>
<p>我们在进行一些赋值，设置变量的时候，可以放在一个块级作用域中保证数据的独立性  <code>with</code> 代码块</p>
</blockquote>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% with %&#125;
	&#123;% set value &#x3D; 42 %&#125;
	&#123;&#123; value &#125;&#125;
&#123;% endwith %&#125;&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>try</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% with %&#125;&#125;
    &#123;% set temp &#x3D; 100 %&#125;
    &#123;% if temp is defined %&#125;
    &lt;p&gt;&#123;&#123; temp &#125;&#125;&lt;&#x2F;p&gt;
    &#123;% else %&#125;
    &lt;p&gt;temp is not defined&lt;&#x2F;p&gt;
    &#123;% endif %&#125;
&#123;% endwith %&#125;&#125;

&#123;% if temp is defined %&#125;
&lt;p&gt;&#123;&#123; temp &#125;&#125;&lt;&#x2F;p&gt;
&#123;% else %&#125;
&lt;p&gt;temp is not defined&lt;&#x2F;p&gt;
&#123;% endif %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212024011.png" srcset="/img/loading.gif" alt="image-20201212024011823"></p>
<h5 id="g-展示原始字符串-raw"><a href="#g-展示原始字符串-raw" class="headerlink" title="g.展示原始字符串 raw"></a>g.展示原始字符串 <code>raw</code></h5><div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% raw %&#125;
........
&#123;% endraw %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% raw %&#125;
    &#123;% if temp is defined %&#125;
    &lt;p&gt;&#123;&#123; temp &#125;&#125;&lt;&#x2F;p&gt;
    &#123;% else %&#125;
    &lt;p&gt;temp is not defined&lt;&#x2F;p&gt;
    &#123;% endif %&#125;
&#123;% endraw %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212024333.png" srcset="/img/loading.gif" alt="image-20201212024333677"></p>
<h4 id="5-4-2-内置过滤器"><a href="#5-4-2-内置过滤器" class="headerlink" title="5.4.2 内置过滤器"></a>5.4.2 内置过滤器</h4><blockquote>
<p>过滤器，就是对数据进行一层修饰，改变它展示的样子，格式化显示</p>
</blockquote>
<blockquote>
<p>学过``vue<code>的同学,它和vue的filter过滤器是一样的,甚至语法都是一样的,使用管道符号 </code>|` 分割</p>
</blockquote>
<p>基本语法</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;&#123;name | filter&#125;&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p>可以链式调用</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;&#123;name | filter1 | filter2 | ...&#125;&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p>可以传递可选参数</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;&#123;name | filter1(params)&#125;&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p>使用标签<code>标签内部的所有变量都会被过滤器修饰</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% filter upper %&#125;
This text becames uppercase
&#123;% endfilter %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p><code>___init__.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/use_filter'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">use_filter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    hello_world <span class="token operator">=</span> <span class="token string">'hello world'</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'use_filter.html'</span><span class="token punctuation">,</span> hello_world<span class="token operator">=</span>hello_world<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>use_filter.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;!doctype html&gt;
&lt;html lang&#x3D;&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;
    &lt;meta name&#x3D;&quot;viewport&quot;
          content&#x3D;&quot;width&#x3D;device-width, user-scalable&#x3D;no, initial-scale&#x3D;1.0, maximum-scale&#x3D;1.0, minimum-scale&#x3D;1.0&quot;&gt;
    &lt;meta http-equiv&#x3D;&quot;X-UA-Compatible&quot; content&#x3D;&quot;ie&#x3D;edge&quot;&gt;
    &lt;title&gt;Document&lt;&#x2F;title&gt;
&lt;&#x2F;head&gt;
&lt;body&gt;
&#123;&#123;hello_world&#125;&#125;-&gt;
&#123;&#123;hello_world|upper&#125;&#125;
&lt;p&gt;############################&lt;&#x2F;p&gt;
&#123;&#123;hello_world&#125;&#125;-&gt;
	&#123;% filter upper %&#125;
	&#123;&#123;hello_world&#125;&#125;
&#123;% endfilter %&#125;
&lt;&#x2F;body&gt;
&lt;&#x2F;html&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>


<p><img src="http://qiniuyun.quancundexiwang.wang/20201212025848.png" srcset="/img/loading.gif" alt="image-20201212025848327"></p>
<p>常用内置过滤器</p>
<ul>
<li><code>abs</code>绝对值 <code>&#123;&#123;value | abs&#125;&#125;</code></li>
<li>默认值,无值存在的默认显示，<ul>
<li><code>default(value,default_value = &#39;&#39;,boolean = False)</code></li>
<li><code>&#123;&#123;value | default('默认值')&#125;&#125;</code></li>
<li><code>&#123;&#123;value | d('默认值')&#125;&#125;</code></li>
</ul>
</li>
<li><code>&#123;&#123;value | e/escape&#125;&#125;</code> html字符串的转义</li>
<li><code>safe</code>富文本展示</li>
<li><code>reserve</code>倒叙</li>
</ul>
<h4 id="5-4-3-自定义过滤器"><a href="#5-4-3-自定义过滤器" class="headerlink" title="5.4.3 自定义过滤器"></a>5.4.3 自定义过滤器</h4><blockquote>
<p>实现电话号码脱敏 13244556677-&gt;132****6677</p>
</blockquote>
<h5 id="a-使用装饰器自定义过滤器"><a href="#a-使用装饰器自定义过滤器" class="headerlink" title="a.使用装饰器自定义过滤器"></a>a.使用装饰器自定义过滤器</h5><div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>template_filter</span><span class="token punctuation">(</span><span class="token string">'filter_name'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">filterFunName</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span><span class="token punctuation">:</span>
  <span class="token keyword">return</span> params<span class="token punctuation">.</span>xxx<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>template_filter</span><span class="token punctuation">(</span><span class="token string">"safe_tel"</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">safe_tel</span><span class="token punctuation">(</span>tel<span class="token punctuation">)</span><span class="token punctuation">:</span>
    tel <span class="token operator">=</span> <span class="token builtin">str</span><span class="token punctuation">(</span>tel<span class="token punctuation">)</span>
    <span class="token keyword">return</span> tel<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">'****'</span> <span class="token operator">+</span> tel<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">:</span><span class="token number">11</span><span class="token punctuation">]</span>
  
 <span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/use_filter'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">use_filter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    hello_world <span class="token operator">=</span> <span class="token string">'hello world'</span>
    tel <span class="token operator">=</span> <span class="token number">13244556677</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'use_filter.html'</span><span class="token punctuation">,</span> hello_world<span class="token operator">=</span>hello_world<span class="token punctuation">,</span> tel<span class="token operator">=</span>tel<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;p&gt;
    &#123;&#123;tel&#125;&#125;-&gt;
    &#123;&#123;tel | safe_tel&#125;&#125;
&lt;&#x2F;p&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212032235.png" srcset="/img/loading.gif" alt="image-20201212032235803"></p>
<h5 id="b-调用函数注册到应用上"><a href="#b-调用函数注册到应用上" class="headerlink" title="b.调用函数注册到应用上"></a>b.调用函数注册到应用上</h5><div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python">app<span class="token punctuation">.</span>jinja_env<span class="token punctuation">.</span>filters<span class="token punctuation">[</span><span class="token string">'filterName'</span><span class="token punctuation">]</span> <span class="token operator">=</span> funcName<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p>定义函数</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">def</span> <span class="token function">safe_tel</span><span class="token punctuation">(</span>tel<span class="token punctuation">)</span><span class="token punctuation">:</span>
    tel <span class="token operator">=</span> <span class="token builtin">str</span><span class="token punctuation">(</span>tel<span class="token punctuation">)</span>
    <span class="token keyword">return</span> tel<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">'****'</span> <span class="token operator">+</span> tel<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">:</span><span class="token number">11</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>定义到app应用上</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">def</span> <span class="token function">safe_tel</span><span class="token punctuation">(</span>tel<span class="token punctuation">)</span><span class="token punctuation">:</span>
    tel <span class="token operator">=</span> <span class="token builtin">str</span><span class="token punctuation">(</span>tel<span class="token punctuation">)</span>
    <span class="token keyword">return</span> tel<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">'****'</span> <span class="token operator">+</span> tel<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">:</span><span class="token number">11</span><span class="token punctuation">]</span>

app<span class="token punctuation">.</span>jinja_env<span class="token punctuation">.</span>filters<span class="token punctuation">[</span><span class="token string">'safe_tel'</span><span class="token punctuation">]</span> <span class="token operator">=</span> safe_tel<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212032506.png" srcset="/img/loading.gif" alt="image-20201212032235803"></p>
<h4 id="5-4-5-模版全局函数"><a href="#5-4-5-模版全局函数" class="headerlink" title="5.4.5 模版全局函数"></a>5.4.5 模版全局函数</h4><ul>
<li><p><code>range</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% for i in range(0,5) %&#125;
&lt;p&gt;&#123;&#123;i&#125;&#125;&lt;&#x2F;p&gt;
&#123;% endfor %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212092403.png" srcset="/img/loading.gif" alt="image-20201212092403709"></p>
</li>
<li><p><code>cycler</code> 生成可迭代的对象，调用<code>next()</code>循环取值,常用语css类名循环</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% set class_name &#x3D; cycler(&#39;row1&#39;,&#39;row2&#39;) %&#125;
&#123;% for i in range(1,10) %&#125;
&lt;p class&#x3D;&quot;&#123;&#123;class_name.next()&#125;&#125;&quot;&gt;
    &#123;&#123;i&#125;&#125;
&lt;&#x2F;p&gt;
&#123;% endfor %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">.row1</span> <span class="token punctuation">&#123;</span>
      <span class="token property">background-color</span><span class="token punctuation">:</span> darkblue<span class="token punctuation">;</span>
      <span class="token property">color</span><span class="token punctuation">:</span> #ffffff<span class="token punctuation">;</span>
   <span class="token punctuation">&#125;</span>
<span class="token selector">.row2</span><span class="token punctuation">&#123;</span>
      <span class="token property">background-color</span><span class="token punctuation">:</span> burlywood<span class="token punctuation">;</span>
      <span class="token property">color</span><span class="token punctuation">:</span> rebeccapurple<span class="token punctuation">;</span>
   <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212093216.png" srcset="/img/loading.gif" alt="image-20201212093216765"></p>
</li>
<li><p><code>joiner(sep=&#39;,&#39;)</code> 用于字符串的拼接</p>
</li>
<li><p><code>url_for</code>   URL地址解析，静态文件地址解析，链接跳转地址解析</p>
<ul>
<li>定义一个css文件，使用<code>url_for</code> 进行静态css文件地址解析，导入到我们的页面中</li>
</ul>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">| |____static
| | |____init.css # 新建的css静态文件
| |____templates
| | |____use_filter.html
| | |____index.html
| | |____brother.html<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>init.css</code></p>
<div class="code-wrapper"><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">body</span> <span class="token punctuation">&#123;</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span> moccasin<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p><code>use_filter.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;link rel&#x3D;&quot;stylesheet&quot; href&#x3D;&quot;&#123;&#123;url_for(&#39;static&#39;,filename &#x3D;&#39;init.css&#39;)&#125;&#125;&quot;&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212094040.png" srcset="/img/loading.gif" alt="image-20201212094040619"></p>
<p>可以看到成功的引入了外部样式</p>
<ul>
<li><p>定义跳转链接 </p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;a href&#x3D;&quot;&#123;&#123; url_for(&#39;brother&#39;) &#125;&#125;&quot;&gt;brother页面&lt;&#x2F;a&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/brother'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">brother</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    NAME <span class="token operator">=</span> <span class="token string">'name'</span>
    AGE <span class="token operator">=</span> <span class="token string">'age'</span>
    ADDRESS <span class="token operator">=</span> <span class="token string">'address'</span>
    brothers <span class="token operator">=</span> <span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"张三"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">22</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"王武"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">29</span><span class="token punctuation">,</span> ADDRESS<span class="token punctuation">:</span> <span class="token string">"通县"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"赵四"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">38</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"刘能"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">36</span><span class="token punctuation">,</span> ADDRESS<span class="token punctuation">:</span> <span class="token string">"铁岭"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"大脑袋"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">44</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token punctuation">&#123;</span>
            NAME<span class="token punctuation">:</span> <span class="token string">"王麻子"</span><span class="token punctuation">,</span> AGE<span class="token punctuation">:</span> <span class="token number">36</span><span class="token punctuation">,</span> ADDRESS<span class="token punctuation">:</span> <span class="token string">"牛栏山"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'brother.html'</span><span class="token punctuation">,</span> brothers<span class="token operator">=</span>brothers<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>当定义这样跳转时，最好视图函数和路由名称一致,比如这里的都叫 brother</code></p>
</li>
</ul>
</li>
</ul>
<h4 id="5-4-6-模版中的宏"><a href="#5-4-6-模版中的宏" class="headerlink" title="5.4.6 模版中的宏"></a>5.4.6 模版中的宏</h4><h5 id="a-宏的基本使用"><a href="#a-宏的基本使用" class="headerlink" title="a.宏的基本使用"></a>a.宏的基本使用</h5><blockquote>
<p>把常用的功能抽离出来，可以重复使用 宏就是函数。</p>
</blockquote>
<blockquote>
<p>宏可以写在单独的html文件中</p>
</blockquote>
<p>我们看下面这样一个模版</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212095956.png" srcset="/img/loading.gif" alt="image-20201212095956685"></p>
<blockquote>
<p>你会发现大部分的代码都是重复可以利用，我们可以吧每一个<code>input</code> 封装成一个模版的宏函数，然后特殊的部分通过传参数的方式进行区分.</p>
</blockquote>
<p>语法</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% macro input(name,value&#x3D;&#39;&#39;,type&#x3D;&#39;text&#39;) %&#125;
    &lt;input type&#x3D;&quot;&#123;&#123; type &#125;&#125;&quot; name&#x3D;&quot;&#123;&#123; name &#125;&#125;&quot; value&#x3D;&quot;&#123;&#123; value &#125;&#125;&quot;&gt;
&#123;% endmacro %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>使用宏函数</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;div&gt;
    &#123;&#123; input(&#39;username&#39;,&#39;asd5628444&#39;) &#125;&#125;
    &#123;&#123; input(&#39;password&#39;,&#39;eerr123ss&#39;,&#39;password&#39;) &#125;&#125;
    &#123;&#123; input(&#39;提交&#39;,&#39;提交&#39;,&#39;submit&#39;) &#125;&#125;
&lt;&#x2F;div&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>页面效果</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212100931.png" srcset="/img/loading.gif" alt="image-20201212100931533"></p>
<h5 id="b-将宏函数单独抽成文件组件"><a href="#b-将宏函数单独抽成文件组件" class="headerlink" title="b.将宏函数单独抽成文件组件"></a>b.将宏函数单独抽成文件组件</h5><p><code>forms.html完整代码</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% macro input(name,value&#x3D;&#39;&#39;,type&#x3D;&#39;text&#39;) -%&#125;
    &lt;input type&#x3D;&quot;&#123;&#123; type &#125;&#125;&quot; name&#x3D;&quot;&#123;&#123; name &#125;&#125;&quot; value&#x3D;&quot;&#123;&#123; value &#125;&#125;&quot;&gt;
&#123;% endmacro %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p><code>user_macro.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;body&gt;
&#123;% from &#39;forms.html&#39; import input as form_input %&#125;
&lt;div&gt;
    &#123;&#123; form_input(&#39;username&#39;,&#39;asd5628444&#39;) &#125;&#125;
    &#123;&#123; form_input(&#39;password&#39;,&#39;eerr123ss&#39;,&#39;password&#39;) &#125;&#125;
    &#123;&#123; form_input(&#39;提交&#39;,&#39;提交&#39;,&#39;submit&#39;) &#125;&#125;
&lt;&#x2F;div&gt;
&lt;&#x2F;body&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<img src="http://qiniuyun.quancundexiwang.wang/20201212100931.png" srcset="/img/loading.gif" alt="">

<h4 id="5-4-7-模版的继承"><a href="#5-4-7-模版的继承" class="headerlink" title="5.4.7 模版的继承"></a>5.4.7 模版的继承</h4><h5 id="a-模版的继承之继承语法"><a href="#a-模版的继承之继承语法" class="headerlink" title="a .模版的继承之继承语法"></a>a .模版的继承之继承语法</h5><blockquote>
<p>这个和vue中插槽完成的功能很相似，就是不同的页面基本布局都是一样的，内容不同，我们就可以把内容写成可变的一块区域，然后对其进行替换。</p>
</blockquote>
<p>​    语法：</p>
<p>​    定义可变区域</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% block content %&#125; 
   内容区域
&#123;% endblock %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>​    继承xxxx页面模版，并且设置插槽内容</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% extends &#39;xxxxx.html&#39; %&#125;
&#123;% block content %&#125;
    xxxxx
&#123;% endblock %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>​    文件目录</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">|____app
| |____init__.py # app应用文件
| |____static
| | |____init.css # 样式文件
| |____templates
| | |____gold_position.html # 金职位
| | |____index.html
| | |____actual_combat.html # 实战课程
| | |____free.html # 免费课程
| | |____mine.html # 个人中心<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>    __init_.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> render_template<span class="token punctuation">,</span> redirect

app <span class="token operator">=</span> Flask<span class="token punctuation">(</span>__name__<span class="token punctuation">)</span>
app<span class="token punctuation">.</span>jinja_env<span class="token punctuation">.</span>add_extension<span class="token punctuation">(</span><span class="token string">'jinja2.ext.loopcontrols'</span><span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> redirect<span class="token punctuation">(</span><span class="token string">"/home"</span><span class="token punctuation">)</span>


<span class="token comment"># 主页</span>
<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/home'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">home_page</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'index.html'</span><span class="token punctuation">)</span>


<span class="token comment"># 实战</span>
<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/actual_combat'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">actual_combat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'actual_combat.html'</span><span class="token punctuation">)</span>


<span class="token comment"># 免费</span>
<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/free'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">free</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'free.html'</span><span class="token punctuation">)</span>


<span class="token comment"># 金职位</span>
<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/gold_position'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">gold_position</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'gold_position.html'</span><span class="token punctuation">)</span>


<span class="token comment"># 个人中心</span>
<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/mine'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">mine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'mine.html'</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>index.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;!doctype html&gt;
&lt;html lang&#x3D;&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;
    &lt;meta name&#x3D;&quot;viewport&quot;
          content&#x3D;&quot;width&#x3D;device-width, user-scalable&#x3D;no, initial-scale&#x3D;1.0, maximum-scale&#x3D;1.0, minimum-scale&#x3D;1.0&quot;&gt;
    &lt;meta http-equiv&#x3D;&quot;X-UA-Compatible&quot; content&#x3D;&quot;ie&#x3D;edge&quot;&gt;
    &lt;link rel&#x3D;&quot;stylesheet&quot; href&#x3D;&quot;&#123;&#123; url_for(&quot;static&quot;,filename&#x3D;&#39;init.css&#39;) &#125;&#125;&quot;&gt;
    &lt;title&gt;Document&lt;&#x2F;title&gt;
&lt;&#x2F;head&gt;
&lt;body&gt;
&lt;header&gt;
    &lt;ul&gt;
        &lt;li&gt;
            &lt;a href&#x3D;&quot;&#123;&#123; url_for(&#39;home_page&#39;) &#125;&#125;&quot;&gt;首页&lt;&#x2F;a&gt;
        &lt;&#x2F;li&gt;
        &lt;li&gt;
            &lt;a href&#x3D;&quot;&#123;&#123; url_for(&#39;actual_combat&#39;) &#125;&#125;&quot;&gt;实战课程&lt;&#x2F;a&gt;
        &lt;&#x2F;li&gt;
        &lt;li&gt;
            &lt;a href&#x3D;&quot;&#123;&#123; url_for(&#39;free&#39;) &#125;&#125;&quot;&gt;免费课程&lt;&#x2F;a&gt;
        &lt;&#x2F;li&gt;
        &lt;li&gt;
            &lt;a href&#x3D;&quot;&#123;&#123; url_for(&#39;gold_position&#39;) &#125;&#125;&quot;&gt;金职位&lt;&#x2F;a&gt;
        &lt;&#x2F;li&gt;
        &lt;li&gt;
            &lt;a href&#x3D;&quot;&#123;&#123; url_for(&#39;mine&#39;) &#125;&#125;&quot;&gt;个人中心&lt;&#x2F;a&gt;
        &lt;&#x2F;li&gt;
    &lt;&#x2F;ul&gt;
&lt;&#x2F;header&gt;
&lt;div class&#x3D;&quot;content&quot;&gt;
    &#123;% block content %&#125; 
        首页内容
    &#123;% endblock %&#125;
&lt;&#x2F;div&gt;
&lt;footer&gt;
    &lt;p&gt;tel:020 65789988&lt;&#x2F;p&gt;
&lt;&#x2F;footer&gt;
&lt;&#x2F;body&gt;
&lt;&#x2F;html&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>​    页面效果</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212113435.png" srcset="/img/loading.gif" alt="image-20201212113435181"></p>
<p>​    <code>actual_combat.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% extends &#39;index.html&#39; %&#125;
&#123;% block content %&#125;
    实战课程
&#123;% endblock %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>free.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% extends &#39;index.html&#39; %&#125;
&#123;% block content %&#125;
    免费课程
&#123;% endblock %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>gold_position.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% extends &#39;index.html&#39; %&#125;
&#123;% block content %&#125;
    金职位
&#123;% endblock %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>mine.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% extends &#39;index.html&#39; %&#125;
&#123;% block content %&#125;
    个人中心
&#123;% endblock %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212114015.gif" srcset="/img/loading.gif" alt="Dec-12-2020 11-39-48"></p>
<p><span style='color:red'>super调用超集</span></p>
<p><code>mine.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% extends &#39;index.html&#39; %&#125;
&#123;% block content %&#125;
    &#123;#  super方法，调用超集的内容  #&#125;
    &#123;&#123; super() &#125;&#125;
    个人中心
&#123;% endblock %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201212114846.png" srcset="/img/loading.gif" alt="image-20201212114846569"></p>
<p>可以看到继承的页面中的内容也出现在了子模板中。</p>
<h5 id="b-模版的继承之包含语法"><a href="#b-模版的继承之包含语法" class="headerlink" title="b.模版的继承之包含语法"></a>b.模版的继承之包含语法</h5><p>语法：</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% include &#39;xxxxx.html&#39; %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>


<blockquote>
<p>继承模版，是继承页面，整个布局，然后修改内容，使用场景，多个页面都有通用的组件，比如头部导航，底部footer,不同的只是其中的内容</p>
<p>包含模版，其实就是封装组件，比如同样的导航条，有的页面有，有的页面没有，这样把导航封装成一个组件，然后需要的页面去引用。</p>
</blockquote>
<p>​    依然用上面的那个页面，我们新增一个问答的页面，带有独特的课程推荐组件，</p>
<p>​        <img src="http://qiniuyun.quancundexiwang.wang/20201213080638.png" srcset="/img/loading.gif" alt="image-20201213080632877"></p>
<p>这个组件我们只希望在问答和个人中心使用，那么我们就要把它封装成一个组件，去这两个页面去使用</p>
<p><code>course_recommend.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>aside</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>right<span class="token punctuation">"</span></span>  <span class="token style-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token style language-css"><span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span><span class="token property">flex-direction</span><span class="token punctuation">:</span> column</span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
            课程推荐
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ol</span>  <span class="token style-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token style language-css"><span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span><span class="token property">flex-direction</span><span class="token punctuation">:</span> column<span class="token punctuation">;</span><span class="token property">list-style</span><span class="token punctuation">:</span> none</span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span>PYTHON<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span>WEB前端<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span>JAVA<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span>MYSQK<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ol</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>aside</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>然后在需要的地方去引入</p>
<p><code>question.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% extends &#39;index.html&#39; %&#125;
&#123;% block content %&#125;
    &lt;p&gt;问答&lt;&#x2F;p&gt;
    &lt;aside class&#x3D;&quot;left&quot;&gt;
            问题列表
    &lt;&#x2F;aside&gt;
    &#123;% include &#39;course_recommend.html&#39; %&#125;
&#123;% endblock %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>参数</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% include &#39;xxxxx.html&#39; ignore missiong %&#125; # 没有找到组件不报错<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% include &#39;xxxxx.html&#39; ignore missiong with context %&#125; # 可以使用父组件的上下文 (默认可以使用的)<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% include &#39;xxxxx.html&#39; ignore missiong without context %&#125; # 不可以使用父组件的上下文<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p>我们模拟从数据库读出用户信息传给页面展示</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment"># 个人中心</span>
<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/mine'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">mine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    user <span class="token operator">=</span> <span class="token string">'张三'</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'mine.html'</span><span class="token punctuation">,</span> user<span class="token operator">=</span>user<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p> 因为<code>mine</code>个人中心使用了<code>course_recommend</code>组件</p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% extends &#39;index.html&#39; %&#125;
&#123;% block content %&#125;
    &#123;#  super方法，调用超集的内容  #&#125;
    &lt;p&gt;个人中心&lt;&#x2F;p&gt;
    &lt;aside class&#x3D;&quot;left&quot;&gt;
        我的信息
    &lt;&#x2F;aside&gt;
    &#123;% include &#39;course_recommend.html&#39;  %&#125;
&#123;% endblock %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>我们将<code>user</code>写到<code>course_recommend</code>中</p>
<div class="code-wrapper"><pre class="line-numbers language-html" data-language="html"><code class="language-html">
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>aside</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>right<span class="token punctuation">"</span></span>  <span class="token style-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token style language-css"><span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span><span class="token property">flex-direction</span><span class="token punctuation">:</span> column</span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
            课程推荐 &#123;&#123; user &#125;&#125;
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ol</span>  <span class="token style-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token style language-css"><span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span><span class="token property">flex-direction</span><span class="token punctuation">:</span> column<span class="token punctuation">;</span><span class="token property">list-style</span><span class="token punctuation">:</span> none</span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span>PYTHON<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span>WEB前端<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span>JAVA<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span>MYSQK<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ol</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>aside</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201213083509.png" srcset="/img/loading.gif" alt="image-20201213083509813"></p>
<h5 id="c-消息闪现"><a href="#c-消息闪现" class="headerlink" title="c.消息闪现"></a>c.消息闪现</h5><blockquote>
<p>对用户的一些提交事件做一些反馈，登陆成功，发帖成功，评论成功等操作。</p>
</blockquote>
<p><code>__init__.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask<span class="token punctuation">,</span> render_template<span class="token punctuation">,</span> redirect<span class="token punctuation">,</span> request<span class="token punctuation">,</span> flash

app<span class="token punctuation">.</span>route<span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">,</span> methods<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'GET'</span><span class="token punctuation">,</span> <span class="token string">'POST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> request<span class="token punctuation">.</span>method <span class="token operator">==</span> <span class="token string">'GET'</span><span class="token punctuation">:</span>
        flash<span class="token punctuation">(</span><span class="token string">'登陆信息失效，请先登陆！'</span><span class="token punctuation">,</span> <span class="token string">'error'</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'login.html'</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        flash<span class="token punctuation">(</span><span class="token string">'登陆成功！'</span><span class="token punctuation">,</span> <span class="token string">'success'</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> redirect<span class="token punctuation">(</span><span class="token string">'/mine'</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>login</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;!DOCTYPE html&gt;
&lt;html lang&#x3D;&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;
    &lt;title&gt;Title&lt;&#x2F;title&gt;
&lt;&#x2F;head&gt;
&lt;body&gt;
&lt;form action&#x3D;&quot;&#123;&#123; url_for(&#39;login&#39;) &#125;&#125;&quot; method&#x3D;&#39;POST&#39;&gt;
    &#123;#  消息闪现是可以多条消息的，所以被放入一个可遍历的数据结构 #&#125;
    &#123;% for msg in get_flashed_messages() %&#125;
        &#123;&#123; msg &#125;&#125;
    &#123;% endfor %&#125;
    &lt;input type&#x3D;&quot;text&quot;&gt;
    &lt;input type&#x3D;&quot;password&quot;&gt;
    &lt;input type&#x3D;&quot;submit&quot;&gt;
&lt;&#x2F;form&gt;
&lt;&#x2F;body&gt;
&lt;&#x2F;html&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201213085719.png" srcset="/img/loading.gif" alt="image-20201213085719891"></p>
<p><code>mine.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% extends &#39;index.html&#39; %&#125;
&#123;% block content %&#125;
    &#123;#  super方法，调用超集的内容  #&#125;
    &lt;p&gt;个人中心&lt;&#x2F;p&gt;

    &lt;aside class&#x3D;&quot;left&quot;&gt;
        我的信息
    &lt;&#x2F;aside&gt;
    &#123;% include &#39;course_recommend.html&#39; %&#125;
      &#123;% for msg in get_flashed_messages() %&#125;
        &lt;p&gt;&#123;&#123; msg &#125;&#125;&lt;&#x2F;p&gt;
    &#123;% endfor %&#125;
&#123;% endblock %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201213085903.gif" srcset="/img/loading.gif" alt="Dec-13-2020 08-58-41"></p>
<p><code>get_flashed_messages</code>函数是可以传递参数的</p>
<ul>
<li><p><code>with_categories = true</code> 是否开启消息分类，开启之后，可以在下一个参数中，过滤我们的消息类型</p>
</li>
<li><p><code>category_filter=[type1,type2,....]</code>  传入type，比如我们上面定义的<code>success,error</code></p>
</li>
</ul>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% for msg in get_flashed_messages(with_categories&#x3D;true,category_filter&#x3D;[&#39;success&#39;]) %&#125;
        &lt;p&gt;&#123;&#123; msg &#125;&#125;&lt;&#x2F;p&gt;
&#123;% endfor %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201213090620.png" srcset="/img/loading.gif" alt="image-20201213090620808"></p>
<p>这样就可以取到类型，我们去多添加几个类型</p>
<p><code>__init__.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">,</span> methods<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'GET'</span><span class="token punctuation">,</span> <span class="token string">'POST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> request<span class="token punctuation">.</span>method <span class="token operator">==</span> <span class="token string">'GET'</span><span class="token punctuation">:</span>
        flash<span class="token punctuation">(</span><span class="token string">'登陆信息失效，请先登陆！'</span><span class="token punctuation">,</span> <span class="token string">'error'</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'login.html'</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        flash<span class="token punctuation">(</span><span class="token string">'登陆成功！'</span><span class="token punctuation">,</span> <span class="token string">'success'</span><span class="token punctuation">)</span>
        flash<span class="token punctuation">(</span><span class="token string">'您的会员即将到期请及时续费！'</span><span class="token punctuation">,</span> <span class="token string">'warning'</span><span class="token punctuation">)</span>
        flash<span class="token punctuation">(</span><span class="token string">'您的信息尚未完善！'</span><span class="token punctuation">,</span> <span class="token string">'info'</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> redirect<span class="token punctuation">(</span><span class="token string">'/mine'</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>mine.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% for msg in get_flashed_messages(with_categories&#x3D;true,category_filter&#x3D;[&#39;success&#39;,&#39;info&#39;]) %&#125;
        &lt;p&gt;&#123;&#123; msg &#125;&#125;&lt;&#x2F;p&gt;
&#123;% endfor %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201213090929.png" srcset="/img/loading.gif" alt="image-20201213090928956"></p>
<p>这样就可以指定出现的类型了。</p>
<p>我们也可以把类型取出来做一些样式操作</p>
<p><code>mine.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&#123;% for c,msg in get_flashed_messages(with_categories&#x3D;true,category_filter&#x3D;[&#39;success&#39;,&#39;info&#39;]) %&#125;
        &lt;p class&#x3D;&quot;&#123;&#123; c &#125;&#125;&quot;&gt;&#123;&#123; c &#125;&#125;-&#123;&#123; msg &#125;&#125;&lt;&#x2F;p&gt;
&#123;% endfor %&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p><code>init.css</code></p>
<div class="code-wrapper"><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">.success</span> <span class="token punctuation">&#123;</span>
    <span class="token property">color</span><span class="token punctuation">:</span> #0f0
<span class="token punctuation">&#125;</span>

<span class="token selector">.error</span> <span class="token punctuation">&#123;</span>
    <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token selector">.info</span><span class="token punctuation">&#123;</span>
    <span class="token property">color</span><span class="token punctuation">:</span> cornflowerblue<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201213091432.png" srcset="/img/loading.gif" alt="image-20201213091432719"></p>
<p>至此，<code>template</code>模版层的基础。</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/python/">python</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/flask/">flask</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/posts/26347/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">喜欢的壁纸，持续置顶更新呢</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/posts/45137/">
                        <span class="hidden-mobile">Python -进程/线程</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments">
                
                
  <div id="vcomments"></div>
  <script type="text/javascript">
    Fluid.utils.waitElementVisible('vcomments', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "Mi65hxq7VAFUDwOLeIGAOgiV-gzGzoHsz",
          app_key: "hMuhiD4FRqhns4giqLiEH9HG",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: false,
          recordIP: false,
          serverURLs: "",
        });
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the
    <a target="_blank" href="https://valine.js.org" rel="nofollow noopener noopener">comments powered by Valine.</a>
  </noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
    

  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        豫ICP备20009912号-1
      </a>
    </span>
    
  </div>


  
</footer>

<!-- SCRIPTS -->

  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":true,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    
      <script  src="/js/lazyload.js" ></script>
    
  



  
    
  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.0/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "http://qiniuyun.quancundexiwang.wang/xmlSearch.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>



</body>
</html>
